package com.gsm.company.service;

import com.gsm.common.service.BaseService;
import com.gsm.common.utils.IdWorker;
import com.gsm.company.dao.DepartmentDao;
import com.gsm.domain.company.Department;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Date;
import java.util.List;

/**
 * @Description: java类作用描述
 * @Author: gaoshengmin
 * @CreateDate: 2020/2/8$ 12:01$
 * @UpdateUser: gaoshengmin
 * @UpdateDate: 2020/2/8$ 12:01$
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Service
public class DepartmentService  extends BaseService<Department> {

    @Autowired
    private DepartmentDao departmentDao;

    @Autowired
    private IdWorker idWorker;

    public void add(Department department) {
        department.setId(idWorker.nextId() + "");
        department.setCreateTime(new Date());
        if (StringUtils.isEmpty(department.getParentId())){
            department.setParentId(department.getId());
        }
        departmentDao.save(department);
    }


    public void update(Department department) {
        Department temp = departmentDao.findById(department.getId()).get();
        temp.setCode(department.getCode());
        temp.setIntroduce(department.getIntroduce());
        temp.setName(department.getName());
        departmentDao.save(temp);
    }

    public List<Department> findAll(String companyId) {
        Specification<Department> spec = getSpec(companyId);
        return departmentDao.findAll(spec);
    }

    public Department findById(String id) {
        return departmentDao.findById(id).get();
    }

    public void deleteById(String id){
        departmentDao.deleteById(id);
    }
}
